﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductionManagement.Application.Command.PreProduction;
using ProductionManagement.Application.Command.PreProductionCommand;
using ProductionManagement.Application.Command.ProductionPlanCommand;
using ProductionManagement.Domain.Dtos.PreProductionDtos;
using ProductionManagement.Domain.Dtos.ProductionPlanDtos;
using ProductionManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductionManagement.Controllers
{
    /// <summary>
    /// 生产计划控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductionPlanController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public ProductionPlanController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        /// 添加预生产计划
        /// </summary>
        /// <param name="command">要添加的实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProductionPlan(ProductionPlanCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.addDto.Office_Id = sid;
                command.addDto.AddName = name;
                command.addDto.AddTime = DateTime.Now;
                command.addDto.User_Id = int.Parse(userId);
                result.Code = 1;
                result.Data = await mediatorp.Send(command);
                result.Message = "添加成功";
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 生产列表
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectProductionPlanDto>>> ProductionPlanShow([FromQuery] ProductionPlanSelectCommand command)
        {
            ApiResult<PageDTO<SelectProductionPlanDto>> result = new ApiResult<PageDTO<SelectProductionPlanDto>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        ///生产删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductionPlanDel(ProductionPlanDelCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.DelName = name;
                cd.DelTime = DateTime.Now;
                result.Code = 200;
                result.Message = "删除成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填生产计划
        /// </summary>
        /// <param name="cd">要返回的id</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<UpdProductionPlanDto>> ProductionPlanFan([FromQuery] ProductionPlanFanCommand cd)
        {
            ApiResult<UpdProductionPlanDto> result = new ApiResult<UpdProductionPlanDto>();
            try
            {
                result.Code = 200;
                result.Message = "反填成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        ///修改生产计划
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductionPlanUpd(ProductionPlanUpdCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.upddto.UpdName = name;
                cd.upddto.UpdTime = DateTime.Now;
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 生产列表2（小美专用1111）
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectProductionPlanDto>>> XMProductionPlanShow([FromQuery] ErProductionPlanSelectCommand command)
        {
            ApiResult<PageDTO<SelectProductionPlanDto>> result = new ApiResult<PageDTO<SelectProductionPlanDto>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
